package com.itheima.bos.service.impl;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.constant.Constants;
import com.itheima.bos.dao.AreaRepository;
import com.itheima.bos.dao.OrderRepository;
import com.itheima.bos.domain.base.Area;
import com.itheima.bos.domain.take_delivery.Order;
import com.itheima.bos.service.OrderService;
import com.itheima.crm.domain.Customer;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderRepository orderRepository;

	@Autowired
	private AreaRepository areaRepository;
	
	@Override
	public void orderSave(Order order) {
		//防止瞬时状态导致异常;
		Area sendArea = order.getSendArea();
		Area sendAreahaveID = areaRepository.findProvinceAndCityAndDistrict(sendArea.getProvince(), sendArea.getCity(), sendArea.getDistrict());
		order.setSendArea(sendAreahaveID);
		Area recArea = order.getRecArea();
		Area recAreahaveID = areaRepository.findProvinceAndCityAndDistrict(recArea.getProvince(), recArea.getCity(), recArea.getDistrict());
		order.setRecArea(recAreahaveID);
		//查询客户的地址与寄件人地址是否相同
		Customer customer = WebClient.create(Constants.CRM_MANAGEMENT_URL + "/services/customerService/find")
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON)
				.get(Customer.class);
		if(customer == null || customer.getAddress() == null){
			return;
		}
		//如果相同
		if(customer.getAddress().equals(order.getSendAddress())){
			//获取客户关联的快递员, 与订单关联
			String fixedAreaId = customer.getFixedAreaId();
		}
	}                        

}
